package br.com.dev.tcc.view;

import java.util.List;

import android.app.Activity;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import br.com.dev.tcc.R;
import br.com.dev.tcc.adapter.GridBaseAdapter;
import br.com.dev.tcc.bean.BaseItem;
import br.com.dev.tcc.exception.GridViewOutOfLayoutException;

/**
 * View padrao para exibicao de uma grade com BaseViewItens
 * 
 * IMPORTANTE!: a activity passada como parâmetro deve conter em seu layout um
 * componente GridView com o id 'grid'
 */
public class GridBaseView {

	protected Activity activity;
	private GridView grid;
	private GridBaseAdapter adapter;
	private List<BaseItem> itens;

	public GridBaseView(Activity activity) throws GridViewOutOfLayoutException{

		this.activity = activity;
		grid = (GridView) activity.findViewById(R.id.grid);
		
		if(grid == null)
			throw new GridViewOutOfLayoutException(activity);
	}

	/**
	 * Adiciona os itens na grade
	 * 
	 * @param itens
	 *            Itens a serem exibidos na GridView
	 */
	public void setGridViewItens(List<BaseItem> itens) {

		this.itens = itens;
		setAdapter();
	}

	private void setAdapter() {

		adapter = new GridBaseAdapter(activity, itens);
		grid.setAdapter(adapter);
	}

	/**
	 * Registra um listener para ser chamado quando ocorrer clique em um dos
	 * itens na grade
	 * 
	 * @param listener
	 */
	public void setOnItemClickListener(OnItemClickListener listener) {

		adapter.setOnItemClickListener(listener);
	}
}
